<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_faq_online_help.php');
class FaqOnlineHelpControl
{
	var $faq_online_help_m;
	function __construct()
	{
	
		$this->faq_online_help_m = new FaqOnlineHelpModule();
	}

	/**
	* 根据id得到faq_info_new信息
	* @param   int         $new_id
	* @return  array
	*/
	function  get_help_id_info($new_id)
	{
		$p_rows  =  array();
		$sql = 'SELECT help.id,help.title,help.submit_ws_id,help.release_ws_id,help.created,help.released,help.version_id,
		help.status,help.hits ,help.is_video,help.sub_title,help.content,help.description,help.keywords,help.is_top FROM 
		online_help AS help   WHERE  help.id = '.$new_id;
		$rows =  $this->faq_online_help_m->get_help_info($sql);
		$rows = $rows[0];
		$arr_version_id = explode(',',$rows['version_id']);//分割以逗号隔开的产品版本id
		$p_version_str = '';
		//通过版本iD得到相应的产品和版本号
		foreach ($arr_version_id AS $v_value)
		{
			$sql = 'SELECT product.product_name ,version.version_name  FROM version,product 
			WHERE version.version_id = '.$v_value.' AND product.product_id = version.product_id  ';
		
			$v_p_rows =  $this->faq_online_help_m->get_help_info($sql);
			$p_rows[trim($v_p_rows[0]['product_name'])]['product_name'] = $v_p_rows[0]['product_name'];
			$version_name = empty($p_rows[trim($v_p_rows[0]['product_name'])]['version_name'])?$v_p_rows[0]['version_name']:','.$v_p_rows[0]['version_name'];
			$p_rows[trim($v_p_rows[0]['product_name'])]['version_name'][] = $version_name;
		
		}

		$rows['product_versions'] = $p_rows;
		return $rows;
	
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @return  array
	*/
	function get_url_info($page,$type = '',$pagesize = 10,$webType = 0,$search_array = null )
	{
		$url_str = $sql_faq = '';
		if($type == 'list')
		{ 
			$sql_faq = 'SELECT COUNT(*) AS total FROM online_help WHERE web_type = '.$webType;
		}
		else
		{
			$data = $this->get_search_condition($search_array,'get_url');
			$url_str = $data['url_str'];
			if(!empty($search_array['p_cbsid']))
			{
				$sql = 'SELECT version_id FROM version WHERE product_cbs_id = '.$search_array['p_cbsid'];
				$result = $GLOBALS['db']->query($sql);
				while($row = $GLOBALS['db']->fetch_array($result))
				{
					$sql = 'SELECT help.id  FROM online_help AS help  WHERE FIND_IN_SET('.$row['version_id'].',help.version_id) AND  help.web_type = '.$webType.' '.$data['where_str'];
					$sql_faq .= empty($sql_faq)?$sql:' UNION '.$sql;
				}
				$sql_faq = empty($sql_faq)?'SELECT COUNT( * ) AS total FROM version WHERE 1 !=1':'SELECT COUNT( * ) AS total FROM ( '.$sql_faq.' ) AS info ';
				
			}else 
			{
				$sql_faq = 'SELECT COUNT(*) AS total FROM online_help AS help  WHERE  help.web_type = '.$webType.'  '.$data['where_str'];
			}
		
		}
		$page_info = get_page_info($sql_faq,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表faq_info_new信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   array      $search_array   搜索数组
	* @return  array
	*/
	function  get_help_info($type,$start = 0,$pagesize = 10,$webType = 0,$search_array = null)
	{
		if($type == 'list')
		{
			$sql_faq = "SELECT help.id,help.title,help.submit_ws_id,help.release_ws_id,help.created,help.released
			,help.status,help.hits ,help.is_video FROM online_help AS help  WHERE  help.web_type = $webType
			 ORDER BY help.id  ASC  LIMIT $start,$pagesize ";
		}else
		{
			$data = $this->get_search_condition($search_array,'search_info');
			if(!empty($search_array['p_cbsid']))
			{
				$sql = 'SELECT version_id FROM version WHERE product_cbs_id = '.$search_array['p_cbsid'];
				$result = $GLOBALS['db']->query($sql);
				while($row = $GLOBALS['db']->fetch_array($result))
				{
					$sql = 'SELECT help.id,help.title,help.submit_ws_id,help.release_ws_id,help.created,help.released
					,help.status,help.hits ,help.is_video FROM online_help AS help  WHERE FIND_IN_SET('.$row['version_id'].
					',help.version_id) AND help.web_type = '.$webType.'  '.$data['where_str'] ;
					$sql_faq .= empty($sql_faq)?$sql:' UNION '.$sql;
				}
				if(empty($sql_faq))
				return array();
				$sql_faq = 'SELECT * FROM ( '.$sql_faq.' ) AS info  ORDER BY id  ASC  LIMIT '.$start.','.$pagesize;
			}else 
			{
				$sql_faq = 'SELECT help.id,help.title,help.submit_ws_id,help.release_ws_id,help.created,help.released
				,help.status,help.hits ,help.is_video FROM online_help AS help  WHERE help.web_type = '.$webType.'  '.$data['where_str'].'
				ORDER BY help.id  ASC  LIMIT '.$start.','.$pagesize ;
			}
		
		}
		return $this->faq_online_help_m->get_help_info($sql_faq);
	}
	/**
	* 得到所有FAQ分类信息
	* @return  array
	*/
	function  get_faq_category_list()
	{
		$sql =  "SELECT type_name,type_id FROM  faq_type  GROUP BY type_name ORDER BY  type_id ASC ";
		return $this->faq_online_help_m->get_new_info($sql);
	}
	/**
	* ajax改变值
	* @param   int      $id               FAQ文章id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  change_info($id,$change_value,$field)
	{
		$sql = "UPDATE faq_info_new SET $field = '$change_value' WHERE id = $id ";
		return $this->faq_online_help_m->get_new_info($sql);
	
	}
	/**
	* 得到在搜索的条件
	*
	* @param   array        $search_array   搜索条件
	* @return  array
	*/
	function get_search_condition($search_array,$type = '')
	{

		$url_str = '&url_page=1'; $where_str = '';
		if(!empty($search_array['id']))
		{
			$where_str = ' AND  help.id = '.$search_array['id'];
			$url_str.= '&help_id='.$search_array['id'];
		}else
		{ 
		
			if(in_array($search_array['is_video'],array('0','1')) )//是否是视频
			{
				
				$where_str .= ' AND help.is_video = '.$search_array['is_video'];
				$url_str.= '&help_video='.$search_array['is_video'];
			}
			if(in_array($search_array['status'],array('0','1')) )//发布状态
			{
				$where_str .= ' AND  help.status = '.$search_array['status'];
				$url_str.= '&help_status='.$search_array['status'];
			}
			if(!empty($search_array['title']))//标题
			{
				$where_str .= " AND help.title LIKE '%{$search_array['title']}%'";
				$url_str.= '&help_title='.urlencode($search_array['title']);
			}
			if(!empty($search_array['p_cbsid']))
			{
			
				$url_str.= '&p_cbsid='.$search_array['p_cbsid'];
			}
		}
		if($type == 'get_url')
		{
			return array('where_str' => $where_str,'url_str' => $url_str);
		}else
		{
			return array('where_str' => $where_str);
		}
	
	}

}
?>